<template>
	<view class="wrapper">

		<swiper class="swiper" indicator-dots autoplay circular indicator-color="#ccc" indicator-active-color="#fff">
			<block v-for="(item, index) in goodsDetail.pics" :key="index">
				<swiper-item @click="previewImg(index)">
					<image class="swiper-img" :src="item.pics_big"></image>
				</swiper-item>
			</block>
		</swiper>
		<!-- 商品信息 -->
		<view class="goods-info">
			<view class="price">￥{{goodsDetail.goods_price}}</view>
			<view class="name-favo">
				<view class="name">{{goodsDetail.goods_name}}</view>
				<view class="favo">
					<text class="iconfont icon-share"></text>
					<text>分享</text>
					<button open-type="share">分享</button>
				</view>
			</view>
			<view class="express">快递: 免运费</view>
		</view>
		<view class="promote">
			<view>
				<text class="name">促销</text>
				<text class="detail">满300减30元</text>
			</view>
			<view class="brother">
				<text class="name">已选</text>
				<text class="detail gray">黑色/S/1件</text>
			</view>
		</view>
		<!-- 图文介绍 -->
		<view class="goods-detail">
			<view class="tabs">
				<text :class="{active:!activeIndex}" @click="activeIndex=0">图文介绍</text>
				<text :class="{active:activeIndex}" @click="activeIndex=1">规格参数</text>
			</view>
			<view class="main">
				<view v-show="!activeIndex" v-html="goodsDetail.goods_introduce">图文介绍</view>
				<!-- <rich-text v-show="!activeIndex" :nodes="goodsDetail.goods_introduce"></rich-text> -->
				<view v-show="activeIndex">商品参数</view>
			</view>
		</view>
		<view class="fixed-bottom">
			<view class="icon-text">
				<text class="iconfont icon-kefu"></text>
				<text>联系客服</text>
				<button open-type="contact">客服</button>
			</view>
			<view class="icon-text" @click="toCart">
				<text class="iconfont icon-cart"></text>
				<text>购物车</text>
			</view>
			<view class="btn add-cart-btn" @click="add2Cart">加入购物车</view>
			<view class="btn buy-btn" @click="toPay">立即购买</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 默认显示图文介绍
				activeIndex: 0,
				// 商品详情
				goodsDetail: {}
			}
		},
		onLoad(options) {
			this.goodsId = options.goodsId
			// 发请求
			this.queryGoodsDetail()
		},
		onShareAppMessage() {
			return {
				title: this.goodsDetail.goods_name,
				imageUrl: this.goodsDetail.pics[0].pics_big,
			}
		},
		methods: {
			// 跳转支付
			toPay() {
				uni.navigateTo({
					url: '/pages/pay/pay?goodsId=' + this.goodsDetail.goods_id
				})
			},
			// add2Cart() {
			// 	//取
			// 	let cart = uni.getStorageSync('cart') || {}

			// 	// 更新
			// 	let goodsId = this.goodsDetail.goods_id
			// 	// 判断是否是第一次
			// 	// if (cart[goodsId]) {
			// 	// 	//非首次, 选中状态true,数量+1
			// 	// 	cart[goodsId] = {
			// 	// 		checked: true,
			// 	// 		num: cart[goodsId].num + 1
			// 	// 	}
			// 	// } else {
			// 	// 	// 首次,选中状态true,数量1
			// 	// 	cart[goodsId] = {
			// 	// 		checked: true,
			// 	// 		num: 1
			// 	// 	}
			// 	// }
			// 	// 方案2,选中状态为true,数量第一次1,非首次+1
			// 	cart[goodsId] = {
			// 		checked: true,
			// 		num: cart[goodsId] ? (cart[goodsId].num + 1) : 1
			// 	}

			// 	// 存
			// 	uni.setStorageSync('cart', cart)

			// },
			add2Cart() {
				// 调用store里面add2Cart mutations
				this.$store.commit('add2Cart', this.goodsDetail.goods_id)
				// 提示
				uni.showToast({
					title: '加入购物车成功！'
				})
			},
			// 跳转购物车页面
			toCart() {
				uni.switchTab({
					url: '/pages/cart/cart'
				})
			},
			// 在新页面中全屏预览图片
			previewImg(index) {
				//遍历pics,取出每一项的图片链接，存到urls里面
				// let urls=[]
				// this.goodsDetail.pics.forEach(item=>{
				// 	urls.push(item.pics_big)
				// })
				// map会返回一个新数组,遍历原数组,每一项执行指定的函数,函数返回值作为新数组的每一项
				let urls = this.goodsDetail.pics.map(item => {
					return item.pics_big
				})

				uni.previewImage({
					current: index, //第一张预览图片索引
					// current:urls[index], //第一张预览图片的链接
					// urls:urls
					urls, //需要预览的图片链接数组
					loop: true
				})
			},
			async queryGoodsDetail() {
				this.goodsDetail = await this.$request({
					url: '/api/public/v1/goods/detail?goods_id=' + this.goodsId
				})
			}
		}
	}
</script>
<style lang="less">
	.iconfont {
		font-size: 40rpx;
	}

	.wrapper {
		background-color: #f4f4f4;
		padding-bottom: 98rpx;
	}

	.swiper {
		height: 720rpx;
	}

	.swiper-img {
		width: 100%;
		height: 720rpx;
	}

	.goods-info {
		padding: 40rpx 0 30rpx 20rpx;
		background-color: #fff;

		.price {
			color: #eb4450;
			font-size: 40rpx;
		}

		.name-favo {
			display: flex;
			height: 78rpx;
			margin-top: 14rpx;

			.name {
				color: #333;
				flex: 1;
				font-size: 28rpx;
				padding-right: 78rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2; // 控制多行的行数
				-webkit-box-orient: vertical;
			}

			.favo {
				border-left: 1px solid #ddd;
				width: 144rpx;
				color: #999;
				font-size: 24rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				position: relative;

				button {
					position: absolute;
					opacity: 0;
				}
			}
		}

		.express {
			color: #999;
			font-size: 24rpx;
			margin-top: 20rpx;
		}
	}

	.promote {
		margin-top: 20rpx;

		view {
			background-color: #fff;
			height: 96rpx;
			line-height: 96rpx;
			font-size: 28rpx;
			margin-top: 20rpx;
			padding-left: 20rpx;

			&.brother {
				margin-top: 0;
			}

			.name {
				color: #404040;
			}

			.detail {
				color: #ff4055;
				margin-left: 40rpx;

				&.gray {
					color: #dfdfdf;
				}
			}
		}
	}

	.goods-detail {
		margin-top: 20rpx;
		background-color: #fff;

		.tabs {
			display: flex;

			text {
				flex: 1;
				text-align: center;
				color: #404040;
				font-size: 22rpx;
				height: 100rpx;
				line-height: 100rpx;
				box-sizing: border-box;

				&.active {
					color: #ff4055;
					border-bottom: 10rpx solid #ff2644;
				}
			}
		}
	}

	.fixed-bottom {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 98rpx;
		display: flex;
		background-color: #fff;

		.icon-text {
			flex: 2;
			font-size: 20rpx;
			color: #404040;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: relative;

			button {
				position: absolute;
				opacity: 0;
			}
		}

		.btn {
			flex: 3;
			color: #fff;
			text-align: center;
			line-height: 98rpx;

			&.add-cart-btn {
				background-color: #ffb400;
			}

			&.buy-btn {
				background-color: #ff2d4a;
			}
		}
	}
</style>
